package com.bestillet.kyllingogco;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import android.annotation.SuppressLint;

public class XmlParser {
	public static ArrayList<Shop> getShops(XmlPullParser parser) throws XmlPullParserException, IOException {
		ArrayList<Shop> shops = new ArrayList<Shop>();
		
		parser.nextTag();
    	parser.require(XmlPullParser.START_TAG, null, "Shops");
    	
    	// go through parser until we get to a end tag
    	while (parser.next() != XmlPullParser.END_TAG) {
    		if (parser.getEventType() != XmlPullParser.START_TAG) {
    			continue;
    		}

    		// if tag name is equal to Shop
    		String tag = parser.getName();
    		if (tag.equals("Shop")) {
    			shops.add(parseShop(parser));
    		} else {
    			getString(parser, tag);
    		}
    		tag = null;
    	}
		
		return shops;
	}
	
	public static Shop parseShop(XmlPullParser parser) throws XmlPullParserException, IOException {
    	parser.require(XmlPullParser.START_TAG, null, "Shop");
    	
    	int version = 1;
    	try {
    		version = Integer.parseInt(parser.getAttributeValue(XmlPullParser.NO_NAMESPACE, "version"));
    	} catch (NumberFormatException e) { }

    	int idx = -1;
    	String name = "";
    	String city = "";
    	String zipcode = "";
    	String address = "";
    	String rabat = "";
    	String logo = "";
    	String hours = "";
    	String orders = "";
    	String smiley = "";
    	String closed = "";
    	String picture = "";
    	
		// go through parser until we get to a end tag
		while (parser.next() != XmlPullParser.END_TAG) {
    		if (parser.getEventType() != XmlPullParser.START_TAG) {
    			continue;
    		}
    		
    		String tag = parser.getName();
    		if (tag.equals("Idx")) {
    			idx = getInt(parser, tag);
    		} else if (tag.equals("Shopname")) {
    			name = getString(parser, tag);
    		} else if (tag.equals("City")) {
    			city = getString(parser, tag);
    		} else if (tag.equals("PostalCode")) {
    			zipcode = getString(parser, tag);
    		} else if (tag.equals("Address")) {
    			address = getString(parser, tag);
    		} else if (tag.equals("Rabat")) {
    			rabat = getString(parser, tag);
    		} else if (tag.equals("Logo")) {
    			logo = getString(parser, tag);
    		} else if (tag.equals("Opentime")) {
    			hours = getString(parser, tag);
    		} else if (tag.equals("Open4orders")) {
    			orders = getString(parser, tag);
    		} else if (tag.equals("Smily")) {
    			smiley = getString(parser, tag);
    		} else if (tag.equals("Closed")) {
    			closed = getString(parser, tag);
    		} else if (tag.equals("Picture")) {
    			picture = getString(parser, tag);
    		} else {
    			getString(parser, tag);
    		}
    		tag = null;
		}
		return new Shop(idx, version, name, city, zipcode, address, rabat, logo, hours, orders, smiley, closed, picture);
	}
	
	public static Shop getShopInfo(XmlPullParser parser) throws XmlPullParserException, IOException {
		parser.nextTag();
    	parser.require(XmlPullParser.START_TAG, null, "ShopInfo");
    	
    	int version = 1;
    	try {
    		version = Integer.parseInt(parser.getAttributeValue(XmlPullParser.NO_NAMESPACE, "version"));
    	} catch (NumberFormatException e) { }
    	
    	int idx = -1;
    	String name = "";
    	String city = "";
    	String zipcode = "";
    	String address = "";
    	String rabat = "";
    	String logo = "";
    	String hours = "";
    	String orders = "";
    	String smiley = "";
    	String closed = "";
    	String picture = "";
    	
		// go through parser until we get to a end tag
		while (parser.next() != XmlPullParser.END_TAG) {
    		if (parser.getEventType() != XmlPullParser.START_TAG) {
    			continue;
    		}
    		
    		String tag = parser.getName();
    		if (tag.equals("Idx")) {
    			idx = getInt(parser, tag);
    		} else if (tag.equals("Shopname")) {
    			name = getString(parser, tag);
    		} else if (tag.equals("City")) {
    			city = getString(parser, tag);
    		} else if (tag.equals("PostalCode")) {
    			zipcode = getString(parser, tag);
    		} else if (tag.equals("Address")) {
    			address = getString(parser, tag);
    		} else if (tag.equals("Rabat")) {
    			rabat = getString(parser, tag);
    		} else if (tag.equals("Logo")) {
    			logo = getString(parser, tag);
    		} else if (tag.equals("Opentime")) {
    			hours = getString(parser, tag);
    		} else if (tag.equals("Open4orders")) {
    			orders = getString(parser, tag);
    		} else if (tag.equals("Smily")) {
    			smiley = getString(parser, tag);
    		} else if (tag.equals("Closed")) {
    			closed = getString(parser, tag);
    		} else if (tag.equals("Picture")) {
    			picture = getString(parser, tag);
    		} else {
    			getString(parser, tag);
    		}
    		tag = null;
		}
		return new Shop(idx, version, name, city, zipcode, address, rabat, logo, hours, orders, smiley, closed, picture);
	}
	
	public static Campaign getCampaign(XmlPullParser parser) throws XmlPullParserException, IOException {
		parser.nextTag();
    	parser.require(XmlPullParser.START_TAG, null, "Campaign");
    	
    	int idx = -1;
    	String overskrift = "";
    	String billede = "";
    	String besked = "";
    	String obs = "";
    	
		// go through parser until we get to a end tag
		while (parser.next() != XmlPullParser.END_TAG) {
    		if (parser.getEventType() != XmlPullParser.START_TAG) {
    			continue;
    		}
    		
    		String tag = parser.getName();
    		if (tag.equals("Idx")) {
    			idx = getInt(parser, tag);
    		} else if (tag.equals("Overskrift")) {
    			overskrift = getString(parser, tag);
    		} else if (tag.equals("Billede")) {
    			billede = getString(parser, tag);
    		} else if (tag.equals("Besked")) {
    			besked = getString(parser, tag);
    		} else if (tag.equals("Obs")) {
    			obs = getString(parser, tag);
    		} else {
    			getString(parser, tag);
    		}
    		tag = null;
		}
		return new Campaign(idx, overskrift, besked, obs, billede);
	}
	
	public static ArrayList<Category> getCategories(XmlPullParser parser) throws XmlPullParserException, IOException {
		ArrayList<Category> categories = new ArrayList<Category>();
		ArrayList<Category> c = new ArrayList<Category>();
		ArrayList<Category> s = new ArrayList<Category>();
		
		parser.nextTag();
    	parser.require(XmlPullParser.START_TAG, null, "KategoriListe");
    	
		// go through parser until we get to a end tag
		while (parser.next() != XmlPullParser.END_TAG) {
    		if (parser.getEventType() != XmlPullParser.START_TAG) {
    			continue;
    		}
    		// if tag name is equal to Category
    		String tag = parser.getName();
    		if (tag.equals("Kategori")) {
    			c.add(parseCategory(parser));
    		} else if (tag.equals("LevelLabel")) {
    			s.add(parseSection(parser));
    		} else {
    			getString(parser, tag);
    		}
    		tag = null;
		}
		
		int i = 0;
		int j = 0;
		while (i < s.size() && j < c.size()) {
			categories.add(s.get(i));
			while (j < c.size() && s.get(i).getLevel() == c.get(j).getLevel()) {
				categories.add(c.get(j));
				j++;
			}
			i++;
		}
		
		return categories;
	}
	
	public static Category parseCategory(XmlPullParser parser) throws XmlPullParserException, IOException {
		parser.require(XmlPullParser.START_TAG, null, "Kategori");
		
		String headline = "";
		String extra = "";
		int categoryId = -1;
		int level = -1;
		
		// go through parser until we get to a end tag
		while (parser.next() != XmlPullParser.END_TAG) {
			if (parser.getEventType() != XmlPullParser.START_TAG) {
				continue;
			}
			
			String tag = parser.getName();
			if (tag.equals("Overskrift"))
				headline = getString(parser, "Overskrift");
			else if (tag.equals("Ekstratxt"))
				extra = getString(parser, "Ekstratxt");
			else if (tag.equals("KategoriId"))
				categoryId = getInt(parser, "KategoriId");
			else if (tag.equals("Level"))
				level = getInt(parser, "Level");
			else
				getString(parser, tag);
			tag = null;
		}
		return new Category(headline, extra, categoryId, level, false);
	}
	
	public static ArrayList<Category> getSections(XmlPullParser parser) throws XmlPullParserException, IOException {
		ArrayList<Category> sections = new ArrayList<Category>();
		
		parser.nextTag();
    	parser.require(XmlPullParser.START_TAG, null, "KategoriListe");
    	
		// go through parser until we get to a end tag
		while (parser.next() != XmlPullParser.END_TAG) {
    		if (parser.getEventType() != XmlPullParser.START_TAG) {
    			continue;
    		}
    		
    		// if tag name is equal to Category
    		String name = parser.getName();
    		if (name.equals("Kategori")) {
    			parseCategory(parser);
    		} else if (name.equals("LevelLabel")) {
    			sections.add(parseSection(parser));
    		} else {
    			getString(parser, name);
    		}
    		name = null;
		}
		return sections;
	}
	
	public static Category parseSection(XmlPullParser parser) throws XmlPullParserException, IOException {
		parser.require(XmlPullParser.START_TAG, null, "LevelLabel");
		
		String text = "";
		int levelId = -1;
		
		// go through parser until we get to a end tag
		while (parser.next() != XmlPullParser.END_TAG) {
			if (parser.getEventType() != XmlPullParser.START_TAG) {
				continue;
			}
			
			String tag = parser.getName();
			if (tag.equals("Kategoritekst"))
				text = getString(parser, "Kategoritekst");
			else if (tag.equals("LevelId"))
				levelId = getInt(parser, "LevelId");
			else
				getString(parser, tag);
			tag = null;
		}
		return new Category(text, null, -1, levelId, true);
	}
	
	public static ArrayList<Menu> getMenu(XmlPullParser parser, int categoryId) throws XmlPullParserException, IOException {
		ArrayList<Menu> menu = new ArrayList<Menu>();
		
		parser.nextTag();
    	parser.require(XmlPullParser.START_TAG, null, "MenuList");
    	
		// go through parser until we get to a end tag
		while (parser.next() != XmlPullParser.END_TAG) {
    		if (parser.getEventType() != XmlPullParser.START_TAG) {
    			continue;
    		}
    		
    		// if tag name is equal to Category
    		String tag = parser.getName();
    		if (tag.equals("Menu")) {
    			menu.add(parseMenu(parser, categoryId));
    		} else {
    			getString(parser, tag);
    		}
    		tag = null;
		}
		return menu;
	}
	
	public static Menu parseMenu(XmlPullParser parser, int categoryId) throws XmlPullParserException, IOException {
		parser.require(XmlPullParser.START_TAG, null, "Menu");
		
		String name = "";
		String picturenormal = "";
		String description = "";
		String obs = "";
		String picturelarge = "";
		int idx = -1;
		int price = -1;
		int mixidx = -1;
		int mixgroup = -1;
		int mixheader = -1;
		
		// go through parser until we get to a end tag
		while (parser.next() != XmlPullParser.END_TAG) {
			if (parser.getEventType() != XmlPullParser.START_TAG) {
				continue;
			}
			
			String tag = parser.getName();
			if (tag.equals("Navn"))
				name = getString(parser, "Navn");
			else if (tag.equals("Picturenormal"))
				picturenormal = getString(parser, "Picturenormal");
			else if (tag.equals("Beskrivelse"))
				description = getString(parser, "Beskrivelse");
			else if (tag.equals("Obs"))
				obs = getString(parser, "Obs");
			else if (tag.equals("Picturelarge"))
				picturelarge = getString(parser, "Picturelarge");
			else if (tag.equals("Idx"))
				idx = getInt(parser, "Idx");
			else if (tag.equals("Pris"))
				price = getInt(parser, "Pris");
			else if (tag.equals("Mixidx"))
				mixidx = getInt(parser, "Mixidx");
			else if (tag.equals("Mixgroup"))
				mixgroup = getInt(parser, "Mixgroup");
			else if (tag.equals("Mixoverskrift"))
				mixheader = getInt(parser, "Mixoverskrift");
			else
				getString(parser, tag);
			tag = null;
		}
		return new Menu(name, picturenormal, description, obs, picturelarge, idx, categoryId, price, mixidx, mixgroup, mixheader);
	}
	
	public static ArrayList<Mix> getMix(XmlPullParser parser) throws XmlPullParserException, IOException {
		ArrayList<Mix> mix = new ArrayList<Mix>();
		
		parser.nextTag();
    	parser.require(XmlPullParser.START_TAG, null, "MixListe");
    	
		// go through parser until we get to a end tag
		while (parser.next() != XmlPullParser.END_TAG) {
    		if (parser.getEventType() != XmlPullParser.START_TAG)
    			continue;
    		
    		// if tag name is equal to Mix
    		String tag = parser.getName();
    		if (tag.equals("Mix"))
    			mix.add(parseMix(parser));
    		else
    			getString(parser, tag);
    		tag = null;
		}
		return mix;
	}
	
	public static Mix parseMix(XmlPullParser parser) throws XmlPullParserException, IOException {
		parser.require(XmlPullParser.START_TAG, null, "Mix");
		
		Mix m = new Mix();
		m.setNavn(parser.getAttributeValue(1));
		m.setLabel(parser.getAttributeValue(3));
		m.setIdx(Integer.parseInt(parser.getAttributeValue(0)));
		m.setType(Integer.parseInt(parser.getAttributeValue(2)));
		
		try {
			int extra = Integer.parseInt(parser.getAttributeValue(4));
			m.setExtra(extra);
		} catch (IllegalArgumentException e) {
			m.setExtra(0);
		}
		
		// go through parser until we get to a end tag
		while (parser.next() != XmlPullParser.END_TAG) {
			if (parser.getEventType() != XmlPullParser.START_TAG)
				continue;
			
			String tag = parser.getName();
			if (tag.equals("Option"))
				m.addOption(parseOption(parser));
			else
				getString(parser, tag);
			tag = null;
		}
		return m;
	}
	
	public static Option parseOption(XmlPullParser parser) throws XmlPullParserException, IOException {
		parser.require(XmlPullParser.START_TAG, null, "Option");
		
		Option o = new Option();
		o.setFill(parser.getAttributeValue(1));
		o.setIdx(Integer.parseInt(parser.getAttributeValue(0)));
		o.setPrice(Integer.parseInt(parser.getAttributeValue(2)));
		o.setSelectable(Boolean.parseBoolean(parser.getAttributeValue(3)));
		parser.nextTag();
		
		return o;
	}
	
	public static ArrayList<MixGroup> getMixGroup(XmlPullParser parser) throws XmlPullParserException, IOException {
		ArrayList<MixGroup> groups = new ArrayList<MixGroup>();
		
		parser.nextTag();
    	parser.require(XmlPullParser.START_TAG, null, "MixGroup");
    	
		// go through parser until we get to a end tag
		while (parser.next() != XmlPullParser.END_TAG) {
    		if (parser.getEventType() != XmlPullParser.START_TAG)
    			continue;
    		
    		// if tag name is equal to Group
    		String tag = parser.getName();
    		if (tag.equals("Group"))
    			groups.add(parseGroup(parser));
    		else
    			getString(parser, tag);
    		tag = null;
		}
		return groups;
	}
	
	public static MixGroup parseGroup(XmlPullParser parser) throws XmlPullParserException, IOException {
		parser.require(XmlPullParser.START_TAG, null, "Group");
		
		String navn = parser.getAttributeValue(1);
		String label = parser.getAttributeValue(3);
		int idx = Integer.parseInt(parser.getAttributeValue(0));
		int antal = Integer.parseInt(parser.getAttributeValue(2));
		
		MixGroup g = new MixGroup(navn, label, idx, 1, antal);
		while (parser.next() != XmlPullParser.END_TAG) {
			if (parser.getEventType() != XmlPullParser.START_TAG)
				continue;
			
			String tag = parser.getName();
			if (tag.equals("Mix")) {
				Mix m = parseMix(parser);
				for (int i = 0; i < 21; i++)
					m.addOption(new Option("" + i, true, -1, 0));
				g.addMix(m);
				m.setGroup(g);
			} else
				getString(parser, tag);
			tag = null;
		}
		return g;
	}
	
	public static ArrayList<MixHeader> getMixHeader(XmlPullParser parser) throws XmlPullParserException, IOException {
		ArrayList<MixHeader> headers = new ArrayList<MixHeader>();
		
		parser.nextTag();
    	parser.require(XmlPullParser.START_TAG, null, "MixHeader");
    	
		// go through parser until we get to a end tag
		while (parser.next() != XmlPullParser.END_TAG) {
    		if (parser.getEventType() != XmlPullParser.START_TAG)
    			continue;
    		
    		// if tag name is equal to Group
    		String tag = parser.getName();
    		if (tag.equals("Header"))
    			headers.add(parseHeader(parser));
    		else
    			getString(parser, tag);
    		tag = null;
		}
		return headers;
	}
	
	public static MixHeader parseHeader(XmlPullParser parser) throws XmlPullParserException, IOException {
		parser.require(XmlPullParser.START_TAG, null, "Header");
		
		String navn = parser.getAttributeValue(1);
		String label = parser.getAttributeValue(2);
		int idx = Integer.parseInt(parser.getAttributeValue(0));
		
		MixHeader h = new MixHeader(navn, label, idx, 1);
		while (parser.next() != XmlPullParser.END_TAG) {
			if (parser.getEventType() != XmlPullParser.START_TAG)
				continue;
			
			String tag = parser.getName();
			if (tag.equals("Mix"))
				h.addMix(parseMix(parser));
			else
				getString(parser, tag);
			tag = null;
		}
		return h;
	}
	
	public static ArrayList<Card> getCards(XmlPullParser parser) throws XmlPullParserException, IOException {
		ArrayList<Card> cards = new ArrayList<Card>();
		
		parser.nextTag();
		parser.require(XmlPullParser.START_TAG, null, "Kort");
		
		while (parser.next() != XmlPullParser.END_TAG) {
			if (parser.getEventType() != XmlPullParser.START_TAG) {
				continue;
			}
			
			String tag = parser.getName();
			if (tag.equals("StempelKort"))
				cards.add(parseCard(parser));
			else
				getString(parser, tag);
			tag = null;
		}
		
		return cards;
	}
	
	public static Card parseCard(XmlPullParser parser) throws XmlPullParserException, IOException {
		parser.require(XmlPullParser.START_TAG, null, "StempelKort");
		Card c = new Card();
		c.setIdx(Integer.parseInt(parser.getAttributeValue(0)));
		c.setName(parser.getAttributeValue(1));
		c.setAmount(Integer.parseInt(parser.getAttributeValue(2)));
		c.setPosition(Integer.parseInt(parser.getAttributeValue(3)));
		parser.nextTag();
		return c;
	}
	
	public static ArrayList<Stamp> getStamps(XmlPullParser parser) throws XmlPullParserException, IOException {
		ArrayList<Stamp> stamps = new ArrayList<Stamp>();
		
		parser.nextTag();
		parser.require(XmlPullParser.START_TAG, null, "Stempler");
		
		while (parser.next() != XmlPullParser.END_TAG) {
			if (parser.getEventType() != XmlPullParser.START_TAG) {
				continue;
			}
			
			String tag = parser.getName();
			if (tag.equals("Stempel"))
				stamps.add(parseStamp(parser));
			else
				getString(parser, tag);
			tag = null;
		}
		
		return stamps;
	}
	
	public static ArrayList<Item> getBasket(XmlPullParser parser) throws XmlPullParserException, IOException {
		ArrayList<Item> basket = new ArrayList<Item>();
		parser.nextTag();
		parser.require(XmlPullParser.START_TAG, null, "Basket");
		
		while (parser.next() != XmlPullParser.END_TAG) {
    		if (parser.getEventType() != XmlPullParser.START_TAG) {
    			continue;
    		}
			
			String tag = parser.getName();
			if (tag.equals("Menu")) {
				basket.add(parseBasketItem(parser));
			} else
				getString(parser, tag);
			tag = null;
		}
		
		return basket;
	}
	
	public static Item parseBasketItem(XmlPullParser parser) throws XmlPullParserException, IOException {
		parser.require(XmlPullParser.START_TAG, null, "Menu");
		Item item = new Item();

		// go through parser until we get to a end tag
		while (parser.next() != XmlPullParser.END_TAG) {
			if (parser.getEventType() != XmlPullParser.START_TAG) {
				continue;
			}

			String tag = parser.getName();
			if (tag.equals("Navn"))
				item.setName(getString(parser, "Navn"));
			else if (tag.equals("Picturenormal"))
				item.setPicturenormal(getString(parser, "Picturenormal"));
			else if (tag.equals("Mix"))
				item.setChoices(getString(parser, "Mix"));
			else if (tag.equals("Idx"))
				item.setIdx(getInt(parser, "Idx"));
			else if (tag.equals("OverskriftId"))
				item.setCategoryId(getInt(parser, "OverskriftId"));
			else if (tag.equals("MenuIdx"))
				item.setMenuIdx(getInt(parser, "MenuIdx"));
			else if (tag.equals("Pris"))
				item.setPrice(getInt(parser, "Pris"));
			else if (tag.equals("Antal"))
				item.setAmount(getInt(parser, "Antal"));
			else
				getString(parser, tag);
			tag = null;
		}
		return item;
	}
	
	public static ArrayList<Order> getOrders(XmlPullParser parser) throws XmlPullParserException, IOException {
		ArrayList<Order> orders = new ArrayList<Order>();
		parser.nextTag();
		parser.require(XmlPullParser.START_TAG, null, "Orders");
		
		while (parser.next() != XmlPullParser.END_TAG) {
    		if (parser.getEventType() != XmlPullParser.START_TAG) {
    			continue;
    		}
			
			String tag = parser.getName();
			if (tag.equals("Order")) {
				orders.add(parseOrder(parser));
			} else
				getString(parser, tag);
			tag = null;
		}
		
		return orders;
	}
	
	public static Order parseOrder(XmlPullParser parser) throws XmlPullParserException, IOException {
		parser.require(XmlPullParser.START_TAG, null, "Order");
		Order order = new Order();

		// go through parser until we get to a end tag
		while (parser.next() != XmlPullParser.END_TAG) {
			if (parser.getEventType() != XmlPullParser.START_TAG) {
				continue;
			}

			String tag = parser.getName();
			if (tag.equals("ShopName"))
				order.setName(getString(parser, "ShopName"));
			else if (tag.equals("City"))
				order.setCity(getString(parser, "City"));
			else if (tag.equals("Date"))
				order.setDate(getString(parser, "Date"));
			else if (tag.equals("OrderIdx"))
				order.setOrderIdx(getInt(parser, "OrderIdx"));
			else if (tag.equals("BasketId"))
				order.setBasketId(getInt(parser, "BasketId"));
			else if (tag.equals("Total"))
				order.setTotal(getInt(parser, "Total"));
			else
				getString(parser, tag);
			tag = null;
		}
		return order;
	}
	
	public static Status getOrderStatus(XmlPullParser parser) throws XmlPullParserException, IOException {
		parser.nextTag();
		parser.require(XmlPullParser.START_TAG, null, "Bestil-Let");
		
		String obs = "";
		String time = "";
		String text = "";
		int status = -1;
		
		while (parser.next() != XmlPullParser.END_TAG) {
    		if (parser.getEventType() != XmlPullParser.START_TAG) {
    			continue;
    		}
			
    		String tag = parser.getName();
			if (tag.equals("Orderstatus"))
				status = getInt(parser, "Orderstatus");
			else if (tag.equals("Obs"))
				obs = getString(parser, "Obs");
			else if (tag.equals("Text"))
				text = getString(parser, "Text");
			else if (tag.equals("Aprovedtime"))
				time = getString(parser, "Aprovedtime");
			tag = null;
		}

		return new Status(time, text, obs, status);
	}
	
	public static Stamp parseStamp(XmlPullParser parser) throws XmlPullParserException, IOException {
		parser.require(XmlPullParser.START_TAG, null, "Stempel");
		Stamp s = new Stamp();
		s.setIdx(Integer.parseInt(parser.getAttributeValue(0)));
		s.setShopid(Integer.parseInt(parser.getAttributeValue(1)));
		s.setStamp(getDate(parser.getAttributeValue(2)));
		parser.nextTag();
		return s;
	}
	
	public static int getBasketId(XmlPullParser parser) throws XmlPullParserException, IOException {
		parser.nextTag();
		parser.require(XmlPullParser.START_TAG, null, "BasketId");
		int idx = getInt(parser, "BasketId");
		return idx;
	}
	
	public static int getItemId(XmlPullParser parser) throws XmlPullParserException, IOException {
		parser.nextTag();
		parser.require(XmlPullParser.START_TAG, null, "ItemId");
		int idx = getInt(parser, "ItemId");
		return idx;
	}
	
	public static int getOrderId(XmlPullParser parser) throws XmlPullParserException, IOException {
		parser.nextTag();
		parser.require(XmlPullParser.START_TAG, null, "OrderId");
		int idx = getInt(parser, "OrderId");
		return idx;
	}
	
	public static int getNumberOfStamps(XmlPullParser parser) throws XmlPullParserException, IOException {
		parser.nextTag();
		parser.require(XmlPullParser.START_TAG, null, "Antal");
		int n = getInt(parser, "Antal");
		return n;
	}
	
	public static boolean getDeleteResponse(XmlPullParser parser) throws XmlPullParserException, IOException {
		parser.nextTag();
		parser.require(XmlPullParser.START_TAG, null, "Success");
		boolean response = getBoolean(parser, "Success");
		return response;
	}
	
	public static int getNotificationId(XmlPullParser parser) throws XmlPullParserException, IOException {
		parser.nextTag();
		parser.require(XmlPullParser.START_TAG, null, "NotificationId");
		int idx = getInt(parser, "NotificationId");
		return idx;
	}
	
	// get the string from the tag
	public static String getString(XmlPullParser parser, String tag) throws XmlPullParserException, IOException {
		String res = "";
		parser.require(XmlPullParser.START_TAG, null, tag);
		if (parser.next() == XmlPullParser.TEXT) {
			res = parser.getText();
			parser.nextTag();
		}
		parser.require(XmlPullParser.END_TAG, null, tag);
		return res;
	}
		
	// parse the string from the tag to char
	public static char getChar(XmlPullParser parser, String tag) throws XmlPullParserException, IOException {
		String s = getString(parser, tag);
		if (s.length() >= 1)
			return s.charAt(0);
		else
			return ' ';
	}
		
	// parse the string from the tag to float
	public static float getFloat(XmlPullParser parser, String tag) throws XmlPullParserException, IOException {
		try {
			float f = Float.parseFloat(getString(parser, tag));
				return f;
		} catch (NumberFormatException e) {
			return -1f;
		}
	}
		
	// parse the string from the tag to float
	public static double getDouble(XmlPullParser parser, String tag) throws XmlPullParserException, IOException {
		try {
			double d = Double.parseDouble(getString(parser, tag));
			return d;
		} catch (NumberFormatException e) {
			return -1;
		}
	}
		
	// parse the string from the tag to int
	public static int getInt(XmlPullParser parser, String tag) throws XmlPullParserException, IOException {
		try {
			int n = Integer.parseInt(getString(parser, tag));
			return n;
		} catch (NumberFormatException e) {
			return -1;
		}
	}
		
	// parse the string from the tag to boolean
	public static boolean getBoolean(XmlPullParser parser, String tag) throws XmlPullParserException, IOException {
		try {
			String bool = getString(parser, tag);
			if (bool.equals("True")) {
				return true;
			} else {
				int n = Integer.parseInt(bool);
				return n == 1;
			}
		} catch (NumberFormatException e) {
			return false;
		}
	}
	
	@SuppressLint("SimpleDateFormat")
	public static Date getDate(String stamp) {
		SimpleDateFormat format = new SimpleDateFormat("dd-MM-yyyy");
		try {
		    return format.parse(stamp);
		} catch (java.text.ParseException e) {
			return null;
		}
	}
}